/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.FileUtils;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.CarBrand;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.service.CarBrandService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 首页品牌Controller
 * @author yangs
 * @version 2017-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/carBrand")
public class CarBrandController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(CarBrandController.class);
	
	@Autowired
	private CarBrandService carBrandService;
	
	@ModelAttribute
	public CarBrand get(@RequestParam(required=false) String id) {
		CarBrand entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = carBrandService.get(id);
		}
		if (entity == null){
			entity = new CarBrand();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:carBrand:view")
	@RequestMapping(value = {"list", ""})
	public String list(CarBrand carBrand, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CarBrand> page = carBrandService.findPage(new Page<CarBrand>(request, response), carBrand); 
		model.addAttribute("page", page);
		return "modules/sys/carBrand/carBrandList";
	}

	@RequiresPermissions("sys:carBrand:view")
	@RequestMapping(value = "form")
	public String form(CarBrand carBrand, Model model) {
		model.addAttribute("carBrand", carBrand);
		return "modules/sys/carBrand/carBrandForm";
	}

	@RequiresPermissions("sys:carBrand:edit")
	@RequestMapping(value = "save")
	public String save(CarBrand carBrand, Model model, RedirectAttributes redirectAttributes,HttpSession session,MultipartFile images ) {
		User us = UserUtils.getUser();
		carBrand.setCreateBy(us);
		carBrand.setUpdateDate(new Date());
		if(images.getSize()>0 ){
		try {
			carBrand.setImg(FileUtils.saveSimpleNewImage(UserUtils.getUser().getId(), images.getOriginalFilename(), images.getInputStream()));
		} catch (IOException e) {
			logger.info("save",e);
		}
		}
		if (!beanValidator(model, carBrand)){
			return form(carBrand, model);
		}
		carBrandService.save(carBrand);
		addMessage(redirectAttributes, "编辑首页品牌成功");
		return "redirect:"+Global.getAdminPath()+"/sys/carBrand/?repage";
	}
	
	@RequiresPermissions("sys:carBrand:edit")
	@RequestMapping(value = "delete")
	public String delete(CarBrand carBrand, RedirectAttributes redirectAttributes) {
		carBrandService.delete(carBrand);
		addMessage(redirectAttributes, "删除首页品牌成功");
		return "redirect:"+Global.getAdminPath()+"/sys/carBrand/?repage";
	}

	
	@RequiresPermissions("sys:carBrand:edit")
	@RequestMapping(value = "brandDetail")
	@ResponseBody
	public String brandDetail(@RequestParam("file") MultipartFile file, HttpSession session) {
		StringBuilder path = new StringBuilder();
		try {
			path.append(FileUtils.saveSimpleNewImage(UserUtils.getUser().getId(), file.getOriginalFilename(), file.getInputStream()));
		} catch (Exception ex) {
			logger.info("brandDetail",ex);
		}
		session.setAttribute("brandImageDetail", path.toString());
		return "true";
	}
}